package com.second.member.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.second.common.pojo.Member;
import com.second.member.dao.MemberDao;
import com.second.member.service.MemberService;
import com.second.member.vo.MemberVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @program: second
 * @description:
 * @author: 胡萌
 * @create: 2023-05-29 14:34
 **/
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberDao memberDao;

    @Override
    public Page<Member> getStudentAll(Integer current, Integer pageSize, MemberVo memberVo) {
        Page<Member> page = new Page<>(current,pageSize);
        QueryWrapper<Member> wrapper = new QueryWrapper<>();

        if (StringUtils.hasText(memberVo.getMobile())){
            wrapper.eq("mobile",memberVo.getMobile());
        }

        if (StringUtils.hasText(memberVo.getNickname())){
            wrapper.like("nickname",memberVo.getNickname());
        }

        if (StringUtils.hasText(memberVo.getIsDisabled())){
            wrapper.eq("is_disabled",memberVo.getIsDisabled());
        }

        if (Objects.nonNull(memberVo.getGmtCreate())){
            wrapper.ge("gmt_create",memberVo.getGmtCreate());
        }

        if (Objects.nonNull(memberVo.getGmtModified())){
            wrapper.le("gmt_modified",memberVo.getGmtModified());
        }

        Page<Member> memberPage = memberDao.selectPage(page, wrapper);

        return memberPage;
    }

    @Override
    public int updateIsDisabled(Integer isDisabled , String id) {
        Member member = new Member();
        member.setId(id);
        member.setIsDisabled(isDisabled);
        member.setGmtModified(LocalDateTime.now());
        int i = memberDao.updateById(member);

        return i;
    }

    @Override
    public int update(Member member) {

        int i = memberDao.updateById(member);

        return i;
    }
}
